package com.wyu.wallet.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wyu.wallet.common.R;
import com.wyu.wallet.entity.BalanceChange;
import com.wyu.wallet.entity.UserWallet;
import com.wyu.wallet.service.BalanceChangeService;
import com.wyu.wallet.service.UserWalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/wallet")
@Slf4j
public class UserController {

    @Autowired
    private UserWalletService userWalletService;

    @Resource
    private BalanceChangeService balanceChangeService;

    //需求一：查询用户钱包余额
    @GetMapping("/{id}")
    public R<UserWallet> get(@PathVariable Integer id) {
        UserWallet userWallet = userWalletService.getById(id);
        if (userWallet != null){
            return R.success(userWallet);
        }else {
            return R.error("没有该用户");
        }
    }

    /**
     @GetMapping("/insert")
     public R<BalanceChange> save(Integer id){
     BalanceChange balanceChange = new BalanceChange();
     BigDecimal b1 = new BigDecimal("100");
     balanceChange.setAmount(b1);
     balanceChange.setId(1);
     balanceChange.setUserId(12);
     balanceChange.setCreateTime(LocalDateTime.now());
     balanceChangeService.save(balanceChange);
     System.out.println("okk");
     return R.success(balanceChange);
     }
     */
    //需求二：用户消费100元的接口
    @GetMapping("/insert")
    public R<BalanceChange> save(@RequestBody Integer id , BigDecimal amount){
        //1.在金额变动表里添加记录
        BalanceChange balanceChange = new BalanceChange();
        BigDecimal b1 = new BigDecimal("-100");
        balanceChange.setAmount(b1);
        balanceChange.setUserId(id);
        balanceChange.setCreateTime(LocalDateTime.now());
        balanceChangeService.save(balanceChange);

        //2.在用户钱包表里修改金额
        UserWallet userWallet = userWalletService.getById(id);
        BigDecimal b = new BigDecimal(String.valueOf(amount));
        BigDecimal result = b.add(b1);
        userWallet.setBalance(result);
        LambdaQueryWrapper<UserWallet> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserWallet::getId, id);
        userWalletService.update(userWallet,lambdaQueryWrapper);



        return R.success(balanceChange);

    }
    //需求三：用户退款20元接口
    @GetMapping("/refund")
    public R<BalanceChange> refund(@RequestBody Integer id , BigDecimal amount){
        //1.在金额变动表里添加记录
        BalanceChange balanceChange = new BalanceChange();
        BigDecimal b1 = new BigDecimal("20");
        balanceChange.setAmount(b1);
        balanceChange.setUserId(id);
        balanceChange.setCreateTime(LocalDateTime.now());
        balanceChangeService.save(balanceChange);


        //2.在用户钱包表里修改金额
        UserWallet userWallet = userWalletService.getById(id);
        BigDecimal b = new BigDecimal(String.valueOf(amount));
        BigDecimal result = b.add(b1);
        userWallet.setBalance(result);
        LambdaQueryWrapper<UserWallet> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserWallet::getId, id);
        userWalletService.update(userWallet,lambdaQueryWrapper);



        return R.success(balanceChange);
    }

    //需求4：查询用户钱包金额变动明细的接口
    @GetMapping("/BalanceChange")
    public R<List<BalanceChange>> list(Integer id){
        //条件构造器
        LambdaQueryWrapper<BalanceChange> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(BalanceChange::getId,id);
        //添加排序条件
        queryWrapper.orderByDesc(BalanceChange::getCreateTime);

        //查询
        List<BalanceChange> list = balanceChangeService.list(queryWrapper);

        return R.success(list);

    }

}
